import React, { Component } from 'react'
import moment from 'moment'
export default class Clock extends Component {
    state={
        timer:'2022-9-16 8:16'
    }

  render() {
    return (
      <div style={{border:'solid 1px #666',display:'inline-block',padding:'20px'}}>
        {this.state.timer}
      </div>
    )
  }
  componentDidMount(){
    console.log('加载完成');
    this.timer = setInterval(()=>{
        this.setState({
            timer:moment().format('YYYY-MM-DD HH:mm:ss')
        })
    })
  }

  componentWillUnmount(){
    console.log('将要进行卸载');
    // 清除定时器
    clearInterval(this.timer)
  }
}
